package com.example.pedometer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements SensorEventListener{
    private SensorManager sensorManager;
    private int step = 0;//步数
    private double oriValue = 0;  //原始值
    private double lastValue = 0;  //上次的值
    private double curValue = 0;  //当前值
    private boolean motiveState = true;   //是否处于运动状态
    private boolean processState = false;   //标记当前是否已经在计步
    Chronometer t_timer;
    Button start,end,zero;
    TextView s_time,e_time,exit_dir,t_step;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

        t_step = (TextView)findViewById(R.id.step);
        t_step.setText("0");
        t_timer = (Chronometer)findViewById(R.id.t_chronometer);
        start = (Button)findViewById(R.id.start);
        end = (Button)findViewById(R.id.end);
        zero = (Button)findViewById(R.id.zero);
        s_time = (TextView)findViewById(R.id.s_time);
        s_time.setText("00:00:00");
        e_time = (TextView)findViewById(R.id.e_time);
        e_time.setText("00:00:00");
        exit_dir = (TextView)findViewById(R.id.exit_dir);

        if (processState == false){
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    t_timer.setBase(SystemClock.elapsedRealtime());
                    t_timer.start();
                    SimpleDateFormat s_simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                    Date date = new Date(System.currentTimeMillis());
                    s_time.setText(s_simpleDateFormat.format(date));
                    processState = true;
                }
            });
        }
        end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleDateFormat e_simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                e_time.setText(e_simpleDateFormat.format(date));
                t_timer.stop();
                processState = false;
            }
        });

        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t_timer.setBase(SystemClock.elapsedRealtime());
                s_time.setText("");
                e_time.setText("");
                s_time.setText("00:00:00");
                e_time.setText("00:00:00");
                step = 0;
                t_step.setText("0");
            }
        });
    }
    @Override
    protected void onResume(){
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_UI);
    }
    @Override
    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }
    @Override
    public void onSensorChanged(SensorEvent event){
        float[] value = event.values;
        int sensorType = event.sensor.getType();
        StringBuilder sb = null;
        switch (sensorType){
            case Sensor.TYPE_ACCELEROMETER:
                double range = 1;   //设定一个精度范围
                curValue = magnitude(value[0], value[1], value[2]);   //计算当前的模
                //向上加速的状态
                if (motiveState == true) {
                    if (curValue >= lastValue) lastValue = curValue;
                    else {
                        //检测到一次峰值
                        if (Math.abs(curValue - lastValue) > range) {
                            oriValue = curValue;
                            motiveState = false;
                        }
                    }
                }
                //向下加速的状态
                if (motiveState == false) {
                    if (curValue <= lastValue) lastValue = curValue;
                    else {
                        if (Math.abs(curValue - lastValue) > range) {
                            //检测到一次峰值
                            oriValue = curValue;
                            if (processState == true) {
                                step++;  //步数 + 1
                                t_step.setText(step + "");    //读数更新
                            }
                            motiveState = true;
                        }
                    }
                }
                break;
            case Sensor.TYPE_ORIENTATION:
                float x = value[2];
                sb = new StringBuilder();
                sb.append(x);
                exit_dir.setText(sb.toString());
                /*// 指向正北
                if(X > 330 - r && X < 360 + r) {
                    exit_dir.setText("北");
                }
                // 指向正东
                else if(X > 90 - r && X < 90 + r) {
                    exit_dir.setText("东");
                }
                // 指向正南
                else if(X > 180 - r && X < 207) {
                    exit_dir.setText("南");
                }
                // 指向正西
                else if(X > 270 - r && X < 270 + r) {
                    exit_dir.setText("西");
                }
                // 指向东北
                else if(X > 0 && X < 45 + r) {
                    exit_dir.setText("东北");
                }
                // 指向东南
                else if(X > 135 - r  && X < 180) {
                    exit_dir.setText("东南");
                }
                // 指向西南
                else if(X > 225 - r && X < 225 + r) {
                    exit_dir.setText("西南");
                }
                // 指向西北
                else if(X > 315 - r && X < 315 + r) {
                    exit_dir.setText("西北");
                }*/
                break;
        }

    }
    //向量求模
    public double magnitude(float x, float y, float z) {
        double magnitude = 0;
        magnitude = Math.sqrt(x * x + y * y + z * z);
        return magnitude;
    }
    @Override
    protected void onStop(){
        sensorManager.unregisterListener(this);
        super.onStop();
    }
}
